home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / COMBOBOX.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  14KB  |  508 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.9  $
  6. //
  7. // Definition of class TComboBox and TComboBoxData the base class for all
  8. // combobox controls.
  9. //----------------------------------------------------------------------------
  10. #if !defined(OWL_COMBOBOX_H)
  11. #define OWL_COMBOBOX_H
  12.  
  13. #if !defined(OWL_LISTBOX_H)
  14. # include <owl/listbox.h>
  15. #endif
  16.  
  17. #if defined(BI_NAMESPACE)
  18. namespace OWL {
  19. #endif
  20.  
  21. // Generic definitions/compiler options (eg. alignment) preceeding the
  22. // definition of classes
  23. #include <services/preclass.h>
  24.  
  25. //
  26. // class TComboBox
  27. // ~~~~~ ~~~~~~~~~
  28. class _OWLCLASS TComboBox : public TListBox {
  29.   public:
  30.     TComboBox(TWindow*        parent,
  31.               int             id,
  32.               int x, int y, int w, int h,
  33.               uint32          style,
  34.               uint            textLimit,
  35.               TModule*        module = 0);
  36.  
  37.     TComboBox(TWindow*   parent,
  38.               int        resourceId,
  39.               uint       textLimit = 0,
  40.               TModule*   module = 0);
  41.    ~TComboBox();
  42.  
  43.     // For combo box's edit subcontrol
  44.     //
  45.     int           GetTextLen() const;
  46.     int           GetText(char far* str, int maxChars) const;
  47.     void          SetText(const char far* str);
  48.  
  49.     uint          GetTextLimit();
  50.     void          SetTextLimit(uint textlimit);
  51.  
  52.     int           GetEditSel(int& startPos, int& endPos);
  53.     int           SetEditSel(int startPos, int endPos);
  54.  
  55.     void          Clear();
  56.  
  57.     // For drop down combo boxes
  58.     //
  59.     void          ShowList(bool show = true);
  60.     void          HideList();
  61.  
  62.     void          GetDroppedControlRect(TRect& Rect) const;
  63.     bool          GetDroppedState() const;
  64.     bool          GetExtendedUI() const;
  65.     int           SetExtendedUI(bool Extended);
  66. #if defined(BI_PLAT_WIN32)
  67.     uint          GetDroppedWidth() const;
  68.     int           SetDroppedWidth(uint width);
  69. #endif
  70.  
  71.     // Combo's List box virtual functions
  72.     //
  73.     virtual int   AddString(const char far* str);
  74.     virtual int   InsertString(const char far* str, int index);
  75.     virtual int   DeleteString(int index);
  76.  
  77.     virtual void  ClearList();
  78.     virtual int   DirectoryList(uint attrs, const char far* fileSpec);
  79.  
  80.     virtual int   GetCount() const;
  81.     virtual int   FindString(const char far* find, int indexStart) const;
  82.  
  83.     virtual int   GetStringLen(int index) const;
  84.     virtual int   GetString(char far* str, int index) const;
  85.  
  86.     virtual int   GetTopIndex() const;
  87.     virtual int   SetTopIndex(int index);
  88.     virtual int   GetHorizontalExtent() const;
  89.     virtual void  SetHorizontalExtent(int horzExtent);
  90.  
  91.     virtual int   GetSelIndex() const;
  92.     virtual int   SetSelIndex(int index);
  93.     virtual int   SetSelString(const char far* findStr, int indexStart);
  94.     virtual uint32 GetItemData(int index) const;
  95.     virtual int   SetItemData(int index, uint32 itemData);
  96.  
  97.     int           GetItemHeight(int index) const;
  98.     int           SetItemHeight(int index, int height);
  99.  
  100. #if defined(BI_PLAT_WIN32)
  101.     int           InitStorage(int numItemsToAdd, uint32 memoryToAllocate);
  102. #endif
  103.  
  104.     // Override TWindow virtual member functions
  105.     //
  106.     uint          Transfer(void* buffer, TTransferDirection direction);
  107.  
  108.   protected:
  109.     // Override TWindow virtual member functions
  110.     //
  111.     char far*     GetClassName();
  112.     void          SetupWindow();
  113.     void          CleanupWindow();
  114.  
  115.   public_data:
  116.     union {
  117. #if defined(OWL2_COMPAT)
  118.       uint  TextLen;    // Obsolete name--use TextLimit below
  119. #endif
  120.       uint  TextLimit;  // Maximum length of text in edit subcontrol
  121.     };
  122.  
  123.   private:
  124.     // Hidden to prevent accidental copying or assignment
  125.     //
  126.     TComboBox(const TComboBox&);
  127.     TComboBox& operator =(const TComboBox&);
  128.  
  129.   DECLARE_STREAMABLE(_OWLCLASS, TComboBox, 1);
  130. };
  131.  
  132. //
  133. // combo box notification macros. methods are: void method()
  134. //
  135. // EV_CBN_CLOSEUP(id, method)
  136. // EV_CBN_DBLCLK(id, method)
  137. // EV_CBN_DROPDOWN(id, method)
  138. // EV_CBN_EDITCHANGE(id, method)
  139. // EV_CBN_EDITUPDATE(id, method)
  140. // EV_CBN_ERRSPACE(id, method)
  141. // EV_CBN_KILLFOCUS(id, method)
  142. // EV_CBN_SELCHANGE(id, method)
  143. // EV_CBN_SELENDCANCEL(id, method)
  144. // EV_CBN_SELENDOK(id, method)
  145. // EV_CBN_SETFOCUS(id, method)
  146.  
  147. //
  148. //  class TComboBoxData
  149. //  ----- -------------
  150. //
  151. class _OWLCLASS TComboBoxData {
  152.   public:
  153.     TComboBoxData();
  154.    ~TComboBoxData();
  155.  
  156.     TStringArray&   GetStrings();
  157.     TDwordArray&    GetItemDatas();
  158.     int             GetSelIndex();
  159.     string&         GetSelection();
  160.  
  161.     void    AddString(const char* str, bool isSelected = false);
  162.     void    AddStringItem(const char* str, uint32 itemData, bool isSelected = false);
  163.     void    Clear();
  164.  
  165.     void    Select(int index);
  166.     void    SelectString(const char far* str);
  167.     int     GetSelCount() const;
  168.     void    ResetSelections();
  169.     int     GetSelStringLength() const;
  170.     void    GetSelString(char far* buffer, int bufferSize) const;
  171.  
  172.   protected:
  173.     TStringArray   Strings;
  174.     TDwordArray    ItemDatas;
  175.     string         Selection;
  176.     int            SelIndex;
  177. };
  178.  
  179. // Generic definitions/compiler options (eg. alignment) following the
  180. // definition of classes
  181. #include <services/posclass.h>
  182.  
  183. #if defined(BI_NAMESPACE)
  184. } // namespace OWL
  185. #endif
  186.  
  187. //----------------------------------------------------------------------------
  188. // Inline implementations
  189. //
  190.  
  191. //
  192. inline int TComboBox::GetTextLen() const {
  193.   PRECONDITION(GetHandle());
  194.   return GetWindowTextLength();
  195. }
  196.  
  197. //
  198. // Return the limit of new characters that can be entered into the edit
  199. // control portion of the combobox.
  200. //
  201. inline uint TComboBox::GetTextLimit() {
  202.   return TextLimit;
  203. }
  204.  
  205. //
  206. inline void TComboBox::HideList() {
  207.   ShowList(false);
  208. }
  209.  
  210. //
  211. // Return number of chars copied
  212. //
  213. inline int TComboBox::GetText(char far* str, int maxChars) const {
  214.   PRECONDITION(GetHandle());
  215.   return GetWindowText(str, maxChars);
  216. }
  217.  
  218. //
  219. // Return CB_ERR if no edit control
  220. //
  221. inline int TComboBox::SetEditSel(int startPos, int endPos) {
  222.   PRECONDITION(GetHandle());
  223.   return (int)SendMessage(CB_SETEDITSEL, 0, MkParam2(startPos, endPos));
  224. }
  225.  
  226. //
  227. // Clear the text
  228. //
  229. inline void TComboBox::Clear() {
  230.   SetText(0);
  231. }
  232.  
  233. //
  234. inline void TComboBox::GetDroppedControlRect(TRect& rect) const {
  235.   PRECONDITION(GetHandle());
  236.   CONST_CAST(TComboBox*,this)->
  237.     SendMessage(CB_GETDROPPEDCONTROLRECT, 0, TParam2((TRect far*)&rect));
  238. }
  239.  
  240. //
  241. inline bool TComboBox::GetDroppedState() const {
  242.   PRECONDITION(GetHandle());
  243.   return (bool)CONST_CAST(TComboBox*,this)->SendMessage(CB_GETDROPPEDSTATE);
  244. }
  245.  
  246. //
  247. inline bool TComboBox::GetExtendedUI() const {
  248.   PRECONDITION(GetHandle());
  249.   return (bool)CONST_CAST(TComboBox*,this)->SendMessage(CB_GETEXTENDEDUI);
  250. }
  251.  
  252. //
  253. inline int TComboBox::SetExtendedUI(bool extended) {
  254.   PRECONDITION(GetHandle());
  255.   return (bool)SendMessage(CB_SETEXTENDEDUI, extended);
  256. }
  257.  
  258. #if defined(BI_PLAT_WIN32)
  259. //
  260. inline uint TComboBox::GetDroppedWidth() const
  261. {
  262.   PRECONDITION(GetHandle());
  263.   return (uint)CONST_CAST(TComboBox*,this)->SendMessage(CB_GETDROPPEDWIDTH);
  264. }
  265.  
  266. //
  267. inline int TComboBox::SetDroppedWidth(uint width)
  268. {
  269.   PRECONDITION(GetHandle());
  270.   return (int)SendMessage(CB_SETDROPPEDWIDTH, width, 0);
  271. }
  272. #endif
  273.  
  274. //
  275. // Add a string to the list part of the associated combobox
  276. // Return index of the string in the list (the first entry is at index 0)
  277. // A negative value is returned if an error occurs
  278. //
  279. inline int TComboBox::AddString(const char far* str) {
  280.   PRECONDITION(GetHandle());
  281.   return (int)SendMessage(CB_ADDSTRING, 0, TParam2(str));
  282. }
  283.  
  284. //
  285. // Insert a string in list part of the associated combobox at the passed
  286. // index, returning the index of the string in the list
  287. // A negative value is returned if an error occurs
  288. //
  289. inline int TComboBox::InsertString(const char far* str, int index) {
  290.   PRECONDITION(GetHandle());
  291.   return (int)SendMessage(CB_INSERTSTRING, index, TParam2(str));
  292. }
  293.  
  294. //
  295. // Delete the string at the passed index in the list part of the associated
  296. // combobox
  297. // Return a count of the entries remaining in the list; A negative value is
  298. // returned if an error occurs
  299. //
  300. inline int TComboBox::DeleteString(int index) {
  301.   PRECONDITION(GetHandle());
  302.   return (int)SendMessage(CB_DELETESTRING, index);
  303. }
  304.  
  305. //
  306. inline int TComboBox::DirectoryList(uint attrs, const char far* fileSpec) {
  307.   PRECONDITION(GetHandle());
  308.   return (int)SendMessage(CB_DIR, attrs, TParam2(fileSpec));
  309. }
  310.  
  311. //
  312. // Clear all the entries in list part of the associated combobox
  313. //
  314. inline void TComboBox::ClearList() {
  315.   PRECONDITION(GetHandle());
  316.   SendMessage(CB_RESETCONTENT);
  317. }
  318.  
  319. //
  320. // Return the number of entries in list part of the associated combobox. A
  321. // negative value is returned if an error occurs
  322. //
  323. inline int TComboBox::GetCount() const {
  324.   PRECONDITION(GetHandle());
  325.   return (int)CONST_CAST(TComboBox*,this)->SendMessage(CB_GETCOUNT);
  326. }
  327.  
  328. //
  329. // Return the index of the first string in list part of the associated
  330. // combobox which begins with the passed string.
  331. //
  332. // Search for a match beginning at the passed SearchIndex. If a match is not
  333. // found after the last string has been compared, the search continues from
  334. // the beginning of the list until a match is found or until the list has been
  335. // completely traversed.
  336. //
  337. // Search from beginning of list when -1 is passed as the index
  338. //
  339. // Return the index of the selected string.  A negative value is returned if an
  340. // error occurs
  341. //
  342. inline int TComboBox::FindString(const char far* find, int indexStart) const {
  343.   PRECONDITION(GetHandle());
  344.   return (int)CONST_CAST(TComboBox*,this)->
  345.            SendMessage(CB_FINDSTRING, indexStart,TParam2(find));
  346. }
  347.  
  348. //
  349. // Retrieve the contents of the string at the passed index of list part of the
  350. // associated combobox, returning the length of the string (in bytes excluding
  351. // the terminating 0) as the value of the call
  352. //
  353. // A negative value is returned if the passed index is not valid
  354. //
  355. // The buffer must be large enough for the string and the terminating 0
  356. //
  357. inline int TComboBox::GetString(char far* str, int index) const {
  358.   PRECONDITION(GetHandle());
  359.   return (int)CONST_CAST(TComboBox*,this)->
  360.            SendMessage(CB_GETLBTEXT, index, TParam2(str));
  361. }
  362.  
  363. //
  364. // Return the length of the string at the passed index in the associated combo
  365. // list excluding the terminating 0
  366. //
  367. // A negative value is returned if an error occurs
  368. //
  369. inline int TComboBox::GetStringLen(int index) const {
  370.   PRECONDITION(GetHandle());
  371.   return (int)CONST_CAST(TComboBox*,this)->
  372.            SendMessage(CB_GETLBTEXTLEN, index);
  373. }
  374.  
  375. //
  376. inline int TComboBox::GetTopIndex() const {
  377.   PRECONDITION(GetHandle());
  378. #if defined(BI_PLAT_WIN32)
  379.   return (int)CONST_CAST(TComboBox*,this)->SendMessage(CB_GETTOPINDEX);
  380. #else
  381.   //
  382.   return -1;
  383. #endif
  384. }
  385.  
  386. //
  387. inline int TComboBox::SetTopIndex(int index) {
  388.   PRECONDITION(GetHandle());
  389. #if defined(BI_PLAT_WIN32)
  390.   return (int)SendMessage(CB_SETTOPINDEX, index);
  391. #else
  392.   //
  393.   return -1;
  394. #endif
  395. }
  396.  
  397. //
  398. inline int TComboBox::GetHorizontalExtent() const {
  399.   PRECONDITION(GetHandle());
  400. #if defined(BI_PLAT_WIN32)
  401.   return (int)CONST_CAST(TComboBox*,this)->SendMessage(CB_GETHORIZONTALEXTENT);
  402. #else
  403.   //
  404.   return -1;
  405. #endif
  406. }
  407.  
  408. //
  409. inline void TComboBox::SetHorizontalExtent(int horzExtent) {
  410.   PRECONDITION(GetHandle());
  411. #if defined(BI_PLAT_WIN32)
  412.   SendMessage(CB_SETHORIZONTALEXTENT);
  413. #else
  414.   //
  415. #endif
  416. }
  417.  
  418. //
  419. inline int TComboBox::GetSelIndex() const {
  420.   PRECONDITION(GetHandle());
  421.   return (int)CONST_CAST(TComboBox*,this)->SendMessage(CB_GETCURSEL);
  422. }
  423.  
  424. //
  425. inline int TComboBox::SetSelIndex(int index) {
  426.   PRECONDITION(GetHandle());
  427.   return (int)SendMessage(CB_SETCURSEL, index);
  428. }
  429.  
  430. //
  431. inline int TComboBox::SetSelString(const char far* findStr, int indexStart) {
  432.   PRECONDITION(GetHandle());
  433.   return (int)SendMessage(CB_SELECTSTRING, indexStart, TParam2(findStr));
  434. }
  435.  
  436. //
  437. inline uint32 TComboBox::GetItemData(int index) const {
  438.   PRECONDITION(GetHandle());
  439.   return CONST_CAST(TComboBox*,this)->SendMessage(CB_GETITEMDATA, index);
  440. }
  441.  
  442. //
  443. inline int TComboBox::SetItemData(int index, uint32 itemData) {
  444.   PRECONDITION(GetHandle());
  445.   return (int)SendMessage(CB_SETITEMDATA, index, itemData);
  446. }
  447.  
  448. //
  449. inline int TComboBox::GetItemHeight(int index) const {
  450.   PRECONDITION(GetHandle());
  451.   return (int)CONST_CAST(TComboBox*,this)->
  452.            SendMessage(CB_GETITEMHEIGHT, index);
  453. }
  454.  
  455. //
  456. inline int TComboBox::SetItemHeight(int index, int height) {
  457.   PRECONDITION(GetHandle());
  458.   return (int)SendMessage(CB_SETITEMHEIGHT, index, MkParam2(height, 0));
  459. }
  460.  
  461. #if defined(BI_PLAT_WIN32)
  462. //
  463. inline int TComboBox::InitStorage(int numItemsToAdd, uint32 memoryToAllocate) {
  464.   PRECONDITION(GetHandle());
  465.   return (int)SendMessage(CB_INITSTORAGE, numItemsToAdd, memoryToAllocate);
  466. }
  467. #endif
  468.  
  469. //
  470. inline TStringArray& TComboBoxData::GetStrings() {
  471.   return Strings;
  472. }
  473.  
  474. //
  475. inline TDwordArray& TComboBoxData::GetItemDatas() {
  476.   return ItemDatas;
  477. }
  478.  
  479. //
  480. inline int TComboBoxData::GetSelIndex() {
  481.   return SelIndex;
  482. }
  483.  
  484. //
  485. inline string& TComboBoxData::GetSelection() {
  486.   return Selection;
  487. }
  488.  
  489. //
  490. inline void TComboBoxData::Clear() {
  491.   Strings.Flush();
  492.   ItemDatas.Flush();
  493.   ResetSelections();
  494. }
  495.  
  496. //
  497. inline int TComboBoxData::GetSelCount() const {
  498.   return SelIndex == CB_ERR ? 0 : 1;
  499. }
  500.  
  501. //
  502. inline void TComboBoxData::ResetSelections() {
  503.   SelIndex = CB_ERR;
  504.   Selection = "";
  505. }
  506.  
  507. #endif  // OWL_COMBOBOX_H
  508.